#pragma once
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <log/log.h>
#include "wmark_dev.h"
#include "Device.h"
#include <iostream>
#include <sys/epoll.h>
#include <utils/Thread.h>
#include "ControlMsgIntface.h"
#define DEV "/dev/wmark_dev_lookback"
#define BUF_FD_MAX DMA_BUFS_MAX_NUM
#define MAX_EVENTS 10

using camera::algorithm::ControlMsgIntface;

namespace android::wmark
{
    class WmarkDev : public Device, public Thread
    {
    private:
        static WmarkDev *instance;
        WmarkDev(/* args */) : Device(DEV, O_WRONLY) {}
        ~WmarkDev() {}
        virtual bool threadLoop() override;
        int getMsgCtl();
        ControlMsgIntface *mCtlMsgCallBack = NULL;

    public:
        static WmarkDev *getInstance()
        {
            if (instance == NULL)
                instance = new WmarkDev();
            return instance;
        }
        // 初始化 ion dma 缓存数量
        int initBufsNum(int num, unsigned long bufLen, int *fds);
        int initWmarkBuf(int num, int w, int h, int ch);

        int getFreeDmaBufIndex();
        int setUseDmaBufIndex(wmark_buf_t &msg);
        int notifyStateChange(state_type_enum state);

        void registerCtlMsgCallback(ControlMsgIntface *callback) {mCtlMsgCallBack = callback;}

        int handlePoll();
        void start();
        void stop();

    private:
        int ioctlDev(unsigned long cmd, void *msg);
        std::mutex mMutex;
    };
}